package org.dromara.demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.ThreadLocalUtils;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.api.services.SystemUserApiService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * 测试token登录
 *
 * @author shuai.zhou
 * @version 3.6.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/login")
public class TestLoginController extends BaseController {

    private final SystemUserApiService systemUserApiService;

    /**
     * 测试feign token传递
     */
    @GetMapping("/test")
    public R<Void> testLogin() {
        log.info("是否登录:{}", StpUtil.isLogin());
        LoginUser loginUser = systemUserApiService.getUserInfo();
        log.info("userInfo:{}", JSONUtil.toJsonStr(loginUser));
        return R.ok();
    }

    /**
     * 测试feign拦截器是否可以获取ThreadLocal值
     */
    @GetMapping("/test/threadLocal")
    public R<Void> testThreadLocal() {
        String uuid = UUID.randomUUID().toString();
        log.info("uuid:{}", uuid);
        ThreadLocalUtils.set("UUID", uuid);
        systemUserApiService.selectListByIds(List.of(1L));
        ThreadLocalUtils.clear();
        return R.ok();
    }

}
